class YcmjGame {
    constructor(data){
        this.state =  data.tid || -1;
        this.baseScore = data.roomConf.baseScore || -1;
        this.maxSeatNum = data.roomConf.maxSeatNum || -1;
        this.isCanBP = data.roomConf.isCanBP || -1;
        this.gameNum = data.roomConf.gameNum || -1;
        this.curgameNum = data.curgameNum || -1;
        this.banker = data.banker || -1;
        this.curOperator = data.curOperator || -1;
        this.creator = data.creator || -1;
        this.lastOperator  =  data.lastOperator || -1;
        this.tid = data.tid || -1;
        this.gameScript = null;
        this.opTime = data.opTime || 0;
       // this.moCard = -1;
        this.seatPlayer = data.seatPlayer || [];
        this.init();
    };
    init(){
        for(let i=0;i<this.seatPlayer.length;i++){
            let sp = this.seatPlayer[i];
            if(sp){
                let handCard = sp.handCard;
                if(handCard instanceof  Array && handCard.length > 0){
                    handCard.sort((a,b)=>{
                        if(this.getType(a) == this.getType(b)){
                            return this.getValue(a) - this.getValue(b);
                        }else{
                            return this.getType(a) - this.getType(b);
                        }
                    });
                };
            };
        };
    };
    getType(data){
        return Math.floor(data / 10);
    };
    getValue(data){
        return data % 10;
    };
    listenMsg(){
        pomelo.on('playerJoin',(data)=>{
            console.log('data------>',data);
            if(data.code != -5){
                let msg = data.msg;
                this.seatPlayer[msg.seatId] = {
                    handCard:[],
                    seatId:msg.seatId,
                    uid:msg.uid,
                    img:msg.img,
                    sex:msg.sex,
                    name:msg.name,
                    score:msg.score,
                    outCard:[],
                    pgCard:[],
                    moCard:-1,
                    state:msg.state
                }; 
                this.gameScript.playerJoin(msg);
            }
        });
        pomelo.on('playerReady',(data)=>{
            console.log('playerReady------>',data);
            if(data.code != -5){
                let msg = data.msg;
                let sp = this.seatPlayer[msg.seatId] ;
                sp.state = sp.state; 
                this.gameScript.playerReady(msg);
            }
        });
        pomelo.on('gameStart',(data)=>{
            console.log('======gameStart======',data);
            if(data.code !=  -5){
                let msg = data.msg;
                this.opTime = msg.opTime;
                for(let i=0;i<this.seatPlayer.length;i++){
                    let sp = this.seatPlayer[i];
                    if(sp) sp.state = 3;
                }
                this.gameScript.gameStart(msg);
            }
        });
        pomelo.on('gameBigSett',(data)=>{
            console.log('======gameBigSett======',data);
            if(data.code !=  -5){
                
            }
        });
        pomelo.on('gameSmallSett',(data)=>{
            console.log('======gameSmallSett======',data);
            if(data.code !=  -5){
                
            }
        });
        pomelo.on('gameDealCard',(data)=>{
            console.log('======gameDealCard====',data);
            if(data.code != -5){
                let msg = data.msg;
                for(let i=0;i<msg.length;i++){
                    let m = msg[i];
                    this.seatPlayer[m.seatId].handCard = m.handCard;
                };
                this.gameScript.gameDealCard(msg);
            }

        });
        pomelo.on('notiyOutCard',(data)=>{
            console.log('======notiyOutCard====',data);
            if(data.code != -5){
                let msg = data.msg;
                this.curOperator = msg.curOperator;
                this.seatPlayer[this.curOperator].moCard = msg.moCard;
                this.lastOperator = msg.lastOperator;
                this.gameScript.notiyOutCard(msg);
            }

        });
        pomelo.on('outCard',(data)=>{
            console.log('======outCard====',data);
            if(data.code != -5){
                let msg = data.msg;
                let handCard = this.seatPlayer[msg.seatId].handCard;
                cc.log('handCard----->',handCard);
                if(handCard instanceof Array && handCard.length > 0){
                    let index = -1 ;
                    for(let i = 0;i< handCard.length;i++){
                        if(msg.card === handCard[i]){
                            index = i;
                            break;
                        };
                    };
                    handCard.splice(index,1); 
                    cc.log('handCard----->',handCard);
                }
                this.gameScript.outCard(msg);
            }
        });
        pomelo.on('gameOperate',(data)=>{
            console.log('======gameOperate====',data);
            if(data.code != -5){
                let msg = data.msg;
                let seatId = msg.seatId;
                if(seatId === this.getMeSeatId()){
                    this.gameScript.opreate(msg.oprate);
                }
            }
        });
        pomelo.on('gameOpration',(data)=>{
            console.log('======gameOpration====',data);
            if(data.code != -5){
                let msg = data.msg;
                this.gameScript.gameOpration(msg);
            }
        });
        
    };
    getMeSeatId(){
        let selfSeatId = -1;
        for(let i=0;i<this.seatPlayer.length;i++){
            let p = this.seatPlayer[i];
            if(p && p.uid == game.user.role.uid){
                selfSeatId = p.seatId;
                break;
            };
        };
        return selfSeatId;
    };
    updateMeHandCard(){
        let seatId = this.getMeSeatId();
        let sp = this.seatPlayer[seatId];
        
    };
    addMeHandCard(){
        let seatId = this.getMeSeatId();
        let sp = this.seatPlayer[seatId];
        if(sp && sp.moCard!= -1){
            let handCard = sp.handCard;
            if(handCard instanceof  Array && handCard.length > 0){
                handCard.push(sp.moCard);
                handCard.sort((a,b)=>{
                    if(this.getType(a) == this.getType(b)){
                        return this.getValue(a) - this.getValue(b);
                    }else{
                        return this.getType(a) - this.getType(b);
                    }
                });
                for(let i = 0 ; i < handCard.length ; i++){
                    if(handCard[i] == sp.moCard){
                        return i;
                    };
                };
                console.log('handCard',handCard);
            };
        };
        return -1;
    };
    convertSeatId(seatId){
        let seatplayer = this.seatPlayer;
        let selfSeatId = -1;
        for(let i=0;i<seatplayer.length;i++){
            let p = seatplayer[i];
            if(p && p.uid == game.user.role.uid){
                selfSeatId = p.seatId;
            };
        };
        let conArry = [];
        conArry.push(selfSeatId);
        while(conArry.length < 4){
            selfSeatId ++;
            if(selfSeatId > 3){
                selfSeatId = 0;
            };
            conArry.push(selfSeatId);
        }
        const index = conArry.findIndex(seat => seat === seatId);
        return index;
    };
    reset(){

    };
    static getInstance(data){
        if(!this.instance){
            this.instance = new YcmjGame(data);
        }; 
        return this.instance;
    };
}
module.exports = YcmjGame;

